package com.imooc.controller;

import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.pojo.vo.ItemCommentLebelCountsVO;
import com.imooc.pojo.vo.ItemInfoVO;
import com.imooc.pojo.vo.ShopcartVO;
import com.imooc.service.ItemService;
import com.imooc.service.StuService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;

@RestController
//@ApiIgnore         //隐藏controller
@RequestMapping("/items")
@Api(value = "商品信息接口",tags = {"items,用于渲染商品信息的相关接口"})
public class ItemsController extends BaseController{

    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "查询商品详情",notes ="查询商品详情",httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public IMOOCJSONResult items(
            @ApiParam(name = "itemId",value = "商品id" ,required = true)
            @PathVariable String itemId){
        if (StringUtil.isEmpty(itemId)){
            return IMOOCJSONResult.errorMsg("商品不存在!");
        }

        Items items=itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgList=itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemsParamList=itemService.queryItemsSpecList(itemId);
        ItemsParam itemsParam=itemService.queryItemsParam(itemId);

        ItemInfoVO itemInfoVO=new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(itemsImgList);
        itemInfoVO.setItemParams(itemsParam);
        itemInfoVO.setItemSpecList(itemsParamList);

        return IMOOCJSONResult.ok(itemInfoVO);
    }

    @ApiOperation(value = "查询商品评价等级数量",notes ="查询商品评价等级数量",httpMethod = "GET")
    @GetMapping("/commentLevel")
    public IMOOCJSONResult commentLevel(
            @ApiParam(name = "itemId",value = "商品id" ,required = true)
            @RequestParam String itemId){

        if (StringUtil.isEmpty(itemId)){
            return IMOOCJSONResult.errorMsg(null);
        }

        ItemCommentLebelCountsVO countsVO=itemService.queryCommentCounts(itemId);

        return IMOOCJSONResult.ok(countsVO);
    }


    @ApiOperation(value = "查询商品评价内容",notes ="查询商品评价内容",httpMethod = "GET")
    @GetMapping("/comments")
    public IMOOCJSONResult comments(
            @ApiParam(name = "itemId",value = "商品id" ,required = true)
            @RequestParam String itemId,
            @ApiParam(name = "level",value = "评价等级" ,required = false)
            @RequestParam Integer level,
            @ApiParam(name = "page",value = "当前页码" ,required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize",value = "页码大小" ,required = false)
            @RequestParam Integer pageSize){

        if (StringUtil.isEmpty(itemId)){
            return IMOOCJSONResult.errorMsg(null);
        }

        if (page==null){
            page=1;
        }

        if (pageSize==null){
            pageSize=COMMON_PAGE_SIZE;
        }

        PagedGridResult countsVO=itemService.queryItemComments(itemId,level,page,pageSize);

        return IMOOCJSONResult.ok(countsVO);
    }

    @ApiOperation(value = "查询搜索商品内容",notes ="查询搜索商品内容",httpMethod = "GET")
    @GetMapping("/search")
    public IMOOCJSONResult search(
            @ApiParam(name = "keywords",value = "商品名称" ,required = true)
            @RequestParam String keywords,
            @ApiParam(name = "sort",value = "排序" ,required = false)
            @RequestParam String sort,
            @ApiParam(name = "page",value = "当前页码" ,required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize",value = "页码大小" ,required = false)
            @RequestParam Integer pageSize){

        if (StringUtil.isEmpty(keywords)){
            return IMOOCJSONResult.errorMsg(null);
        }

        if (page==null){
            page=1;
        }

        if (pageSize==null){
            pageSize=SEARCH_PAGE_SIZE;
        }

        PagedGridResult grid=itemService.searchItems(keywords,sort,page,pageSize);

        return IMOOCJSONResult.ok(grid);
    }


    @ApiOperation(value = "根据分类Id搜索商品内容",notes ="根据分类Id搜索商品内容",httpMethod = "GET")
    @GetMapping("/catItems")
    public IMOOCJSONResult catItems(
            @ApiParam(name = "catId",value = "三级分类Id" ,required = true)
            @RequestParam Integer catId,
            @ApiParam(name = "sort",value = "排序" ,required = false)
            @RequestParam String sort,
            @ApiParam(name = "page",value = "当前页码" ,required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize",value = "页码大小" ,required = false)
            @RequestParam Integer pageSize){

        if (catId==null){
            return IMOOCJSONResult.errorMsg(null);
        }

        if (page==null){
            page=1;
        }

        if (pageSize==null){
            pageSize=SEARCH_PAGE_SIZE;
        }

        PagedGridResult grid=itemService.searchItemsByThirdCat(catId,sort,page,pageSize);

        return IMOOCJSONResult.ok(grid);
    }


    //用于用户长时间未登陆网站，属性购物车中的数据（主要是商品价格），类似京东淘宝
    @ApiOperation(value = "根据商品规格Ids查找最新的商品数据",notes ="根据商品规格Ids查找最新的商品数据",httpMethod = "GET")
    @GetMapping("/refresh")
    public IMOOCJSONResult refresh(
            @ApiParam(name = "itemSpecIds",value = "拼接的规格ids" ,required = true,example = "1001,1003,1005")
            @RequestParam String itemSpecIds){

        if (StringUtils.isEmpty(itemSpecIds)){
            return IMOOCJSONResult.ok();
        }

        List<ShopcartVO> list=itemService.queryItemsBySpecIds(itemSpecIds);

        return IMOOCJSONResult.ok(list);
    }

}
